package com.studio.julianne.action;

import java.io.IOException;
import java.util.Collection;
import java.util.logging.Logger;

import javax.servlet.http.*;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.ValidationAwareSupport;
import com.studio.julianne.dao.StudentDAO;
import com.studio.julianne.dao.StudentDAOImpl;
import com.studio.julianne.model.Student;

@Results({
    @Result(name="success", type="redirectAction", params = {"actionName" , "student"})
})
public class StudentAction extends ValidationAwareSupport implements ModelDriven<Object>, Validateable{
	private static final Logger log = Logger.getLogger(StudentAction.class.getName());

    private Student student = new Student();
    private String id;
    private Collection<Student> list;
    private StudentDAO studentDAO = new StudentDAOImpl();

    // GET /orders/1
    public HttpHeaders show() {
        return new DefaultHttpHeaders("show");
    }

    // GET /orders
    public HttpHeaders index() {
        list = studentDAO.selectAll();
        return new DefaultHttpHeaders("index")
            .disableCaching();
    }
    
    // GET /orders/1/edit
    public String edit() {
        return "edit";
    }

    // GET /orders/new
    public String editNew() {
        student = new Student();
        return "editNew";
    }

    // GET /orders/1/deleteConfirm
    public String deleteConfirm() {
        return "deleteConfirm";
    }

    // DELETE /orders/1
    public String destroy() {
        studentDAO.delete(id);
        addActionMessage("Order removed successfully");
        return "success";
    }

    // POST /orders
    public HttpHeaders create() {
        studentDAO.persist(student);
        addActionMessage("New order created successfully");
        return new DefaultHttpHeaders("success")
            .setLocationId(student.getId());
    }

    // PUT /orders/1
    public String update() {
        studentDAO.persist(student);
        addActionMessage("Order updated successfully");
        return "success";
    }

    public void validate() {
        if (student.getFirstName() == null || student.getFirstName().length() ==0) {
            addFieldError("clientName", "The client name is empty");
        }
    }

    public void setId(String id) {
        if (id != null) {
            this.student = studentDAO.get(id);
        }
        this.id = id;
    }
    
    public Object getModel() {
        return (list != null ? list : student);
    }

}
